package clasesGraficas;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;

import android.graphics.Color;
import android.graphics.Paint.Align;

public class Estados {
	//### VARIABLES ###
	
	//Varibles del activity
		
	private XYMultipleSeriesDataset dataset = null;
	private XYMultipleSeriesRenderer mRenderer = null;
	private String[] meses = {"Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio",
	    					      "Agosto", "Septiembre", "Noviembre", "Diciembre"};
		
	//Variables que son pasadas al activity
	private List<double[]> eventos = null;
	private String[] titulos = null;
	private int[] colors = null;
	
	//Pintar reporte (tipo es: 1= anual, 2= trimestrual/mensual)
	
	
	public void ejecutar(List<double[]> eventos, String[] titulos) {			
		this.titulos = titulos;
		this.eventos = eventos;
		
		colors = new int[eventos.size()];
		colors[0] = Color.BLUE;
		colors[1] = Color.GREEN;
		colors[2] = Color.YELLOW;
		colors[3] = Color.RED;
		
		/*int [] color;
		for (int i=0;i<colors.length;i++){
			color = dameAleatorios();
			colors[i] = Color.argb(255,color[1],color[2],color[3]);
		}*/
		    
		mRenderer = buildBarRenderer(colors);
		mRenderer.setOrientation(Orientation.HORIZONTAL);
	    setChartSettings(mRenderer, "Nro de Eventos por estado", "Mes", "Nro Eventos", Color.GRAY, Color.LTGRAY);
	    
	    dataset = buildBarDataset(titulos, this.eventos);
	}	
	
	    //return ChartFactory.getBarChartView(context, buildBarDataset(titles, values), renderer,
	      //  Type.STACKED);

	protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
		      String yTitle, int axesColor,
		      int labelsColor) 
	{
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
		renderer.setMargins(new int[] {50, 50, 50, 50}); //{derecha, arriba, izquierda, abajo}
		renderer.setBarSpacing(1);
		renderer.setShowGrid(true);
		renderer.setGridColor(Color.BLACK);
		renderer.setXLabels(10); //Cantidad de lineas
		renderer.setYLabels(10); //Cantidad de lineas
		//renderer.setZoomRate(1.1f);
		//renderer.setZoomButtonsVisible(true);
		renderer.setClickEnabled(true);
		
		//ESTO VA TENER QUE CAMBIAR PARA QUE SEA DINAMICO
		renderer.setXAxisMin(0);
		renderer.setXAxisMax(6);
		renderer.setYAxisMin(0);
		renderer.setYAxisMax(1500);
		
	}
	
	protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) 
	{
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
	    renderer.setAxisTitleTextSize(30);
	    renderer.setChartTitleTextSize(30);
	    renderer.setLabelsTextSize(15);
	    renderer.setLegendTextSize(25);
	    
	    int length = colors.length;
	    for (int i = 0; i < length; i++)
	    {
	    	SimpleSeriesRenderer r = new SimpleSeriesRenderer();
	    	r.setColor(colors[i]);
	    	r.setChartValuesSpacing(2);
	    	r.setDisplayChartValues(true);
	    	r.setChartValuesTextAlign(Align.LEFT);
	    	r.setChartValuesTextSize(24);
	    	renderer.addSeriesRenderer(r);
	    }
	    
	    return renderer;
	  }
	
	protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) 
	{
	    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
	    int length = titles.length;
	    
	    for (int i = 0; i < length; i++)
	    {
	    	CategorySeries series = new CategorySeries(titles[i]);
	    	double[] v = values.get(i);
	    	int seriesLength = v.length;
	    	
	    	for (int k = 0; k < seriesLength; k++) 
	    	{
	    		series.add(v[k]);
	    	}
	    	dataset.addSeries(series.toXYSeries());
	    }
	    
	    return dataset;
	  }

	public XYMultipleSeriesDataset getDataset() {
		return dataset;
	}

	public void setDataset(XYMultipleSeriesDataset dataset) {
		this.dataset = dataset;
	}

	public XYMultipleSeriesRenderer getmRenderer() {
		return mRenderer;
	}

	public void setmRenderer(XYMultipleSeriesRenderer mRenderer) {
		this.mRenderer = mRenderer;
	}
	
	public int[] dameAleatorios(){
		int [] numeros = new int[4];
		
		for (int i=0;i<4;i++)
			numeros[i] = (int)(Math.random()*255);
		
		return numeros;
	}

}
